package com.tanhua.dubbo.api.impl.mongo;
import org.bson.types.ObjectId;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 200000)
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RecommendUserApi recommendUserApi;

    @Override
    public Long queryLoveCount(Long userId) {
        // 查询喜欢数据 db.user_like.find({userId:1}).count()
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryFanCount(Long userId) {
        // 查询粉丝 db.user_like.find({likeUserId:1}).count()
        Query query = new Query(
                Criteria.where("likeUserId").is(userId)
        );
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryEachLoveCount(Long userId) {
        // 查询互相喜欢 db.tanhua_users.find({userId:1})
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        return mongoTemplate.count(query, Friend.class);
    }



    // 查询喜欢数据 db.user_like.find({userId:1})
    @Override
    public PageResult queryLoveList(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        query.skip((page-1)*pagesize).limit(pagesize);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        // 为了统一返回查询结果，现在把List<UserLike>转换为List<Map<String,Object>>
        List<Map<String,Object>> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (UserLike userLike : list) {
                Map<String,Object> map = new HashMap<>();
                // 存放我喜欢的用户id，也是返回的喜欢用户
                map.put("uid",userLike.getLikeUserId());
                // 查询并设置缘分值
                Long score = recommendUserApi.queryScore(userLike.getUserId(), userLike.getLikeUserId());
                map.put("score",score);
                // 添加到集合
                resultList.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,resultList);
    }

    // 查询粉丝 db.user_like.find({likeUserId:1})
    @Override
    public PageResult fanList(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(
                Criteria.where("likeUserId").is(userId)
        );
        query.skip((page-1)*pagesize).limit(pagesize);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        // 为了统一返回查询结果，现在把List<UserLike>转换为List<Map<String,Object>>
        List<Map<String,Object>> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (UserLike userLike : list) {
                Map<String,Object> map = new HashMap<>();
                // 存放我喜欢的用户id，也是返回的喜欢用户
                map.put("uid",userLike.getUserId());
                // 查询并设置缘分值
                Long score = recommendUserApi.queryScore(userLike.getLikeUserId(),userLike.getUserId());
                map.put("score",score);
                // 添加到集合
                resultList.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,resultList);
    }

    // 查询互相喜欢 db.tanhua_users.find({userId:1})
    @Override
    public PageResult queryEachLoveList(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Friend> list = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);

        // 为了统一返回查询结果，现在把List<UserLike>转换为List<Map<String,Object>>
        List<Map<String,Object>> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (Friend friend : list) {
                Map<String,Object> map = new HashMap<>();
                map.put("uid",friend.getFriendId());
                // 查询并设置缘分值
                Long score = recommendUserApi.queryScore(friend.getUserId(), friend.getFriendId());
                map.put("score",score);
                // 添加到集合
                resultList.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,resultList);
    }


    // 查询互相喜欢 db.visitors.find({userId:1})
    @Override
    public PageResult visitorList(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Visitors> list = mongoTemplate.find(query, Visitors.class);
        long count = mongoTemplate.count(query, Visitors.class);

        // 为了统一返回查询结果，现在把List<UserLike>转换为List<Map<String,Object>>
        List<Map<String,Object>> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (Visitors visitors : list) {
                Map<String,Object> map = new HashMap<>();
                map.put("uid",visitors.getVisitorUserId());
                // 查询并设置缘分值
                Long score = recommendUserApi.queryScore(visitors.getUserId(), visitors.getVisitorUserId());
                map.put("score",score);
                // 添加到集合
                resultList.add(map);
            }
        }
        return new PageResult(page,pagesize, (int) count,resultList);
    }

    @Override
    public void remove(Long userId, Long likeUserId) {
        //db.user_like.remove({userId:6,likeUserId:6})
        Query query = new Query(
            Criteria.where("userId").is(userId)
            .and("likeUserId").is(likeUserId)
        );
        mongoTemplate.remove(query,UserLike.class);
    }

    /**
     * 保存喜欢关系
     *
     * @param userId
     * @param loveId
     */
    @Override
    public void save(Long userId, Long loveId) {
        //创建一个喜欢对象
        UserLike userLike = new UserLike();
        userLike.setId(new ObjectId());
        //用户
        userLike.setUserId(userId);
        //喜欢的用户
        userLike.setLikeUserId(loveId);
        userLike.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userLike);

    }
}
